package hhAcademicBowl;

import java.awt.Dimension;
import java.awt.RenderingHints.Key;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class hhacGui2 {
	
	HHAC manager;
	JFrame frame;
	
	int qsAsked = 0; //when this is 25 the HHAC class will switch to DJ or FJ
	
	public void show(){
		((JLabel) frame.getContentPane().getComponent(1)).setText("Red Score: " + manager.getRedScore());
		((JLabel) frame.getContentPane().getComponent(2)).setText("Black Score: " + manager.getBlackScore());
		frame.setVisible(true);
	}

	/**
	 * Create the application.
	 */
	public hhacGui2(HHAC man) {
		manager = man;
		initialize();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setBackground(ColorConstants.BG);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		//Dimension screen = new Dimension(1080,720);
		frame.setBounds(0,0,screen.width,screen.height);
		frame.setUndecorated(true); //maximized
		
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		
		
		JLabel lblTitle = new JLabel("HH Academic Bowl");
		lblTitle.setHorizontalAlignment(SwingConstants.LEFT);
		lblTitle.setFont(new Font("Impact", Font.PLAIN, 69));
		lblTitle.setBounds((frame.getWidth()-525)/2, 10, 525, 68); //title position
		frame.getContentPane().add(lblTitle);
		
		JLabel lblRedScore = new JLabel("Red Score: 0");
		lblRedScore.setFont(new Font("Tahoma", Font.PLAIN, 35));
		lblRedScore.setBounds(65, 10, 290, 70);
		frame.getContentPane().add(lblRedScore);
		
		JLabel lblBlackScore = new JLabel("Black Score: 0");
		lblBlackScore.setFont(new Font("Tahoma", Font.PLAIN, 35));
		lblBlackScore.setBounds( ((frame.getWidth()-525)/2) + 555 , 10, 290, 70);
		frame.getContentPane().add(lblBlackScore);
		
		
		
		
		JPanel gamePanel = new JPanel();
		gamePanel.setForeground(Color.BLACK);
		gamePanel.setBackground(ColorConstants.BG);
		gamePanel.setBounds(65, (lblTitle.getY() + lblTitle.getHeight() + 10), (frame.getWidth()-65*2), (frame.getHeight()-65*2));
		frame.getContentPane().add(gamePanel);
		gamePanel.setLayout(null);
		
		JButton btnEndGame = new JButton("End Game");
		btnEndGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				manager.endGame();
			}
		});
		btnEndGame.setBounds(frame.getWidth()-150, frame.getHeight()-35, 125, 25);
		frame.getContentPane().add(btnEndGame);
		
		
		int categoryWidth = ((gamePanel.getWidth()-60)/5);
		JPanel[] categoryPanels = new JPanel[5];
		JLabel[] categoryLabels = new JLabel[5];
		String[] categories;
		if(manager.DoubleJeopardy){
			categories = manager.currentGame.doubleCategories;
		}
		else{
			categories = manager.currentGame.normalCategories;
		}
		
		//---------------------------------------------Category Paneling--------------------------------------------------------//
		for (int i = 0; i < categoryPanels.length; i++) {
			categoryPanels[i] = new JPanel();
			categoryPanels[i].setBackground(ColorConstants.BG);
			if(i<1){
				categoryPanels[i].setBounds(10, 0, categoryWidth, gamePanel.getHeight());
			}
			else{
				categoryPanels[i].setBounds(categoryPanels[i-1].getX()+categoryWidth+10, 0, categoryWidth, gamePanel.getHeight());
			}
			gamePanel.add(categoryPanels[i]);
			categoryPanels[i].setLayout(null);
			
			
			categoryLabels[i] = new JLabel(categories[i]);
			categoryLabels[i].setHorizontalAlignment(SwingConstants.CENTER);
			categoryLabels[i].setBounds(0, 0, categoryWidth, 38);
			categoryLabels[i].setFont(new Font("Impact", Font.PLAIN, 30));
			categoryPanels[i].add(categoryLabels[i]);
			
			//Buttons loops and handling
			int buttonBuffer = (categoryPanels[i].getHeight() - (38) - (85 * 5)) / 5;
			JButton[] buttonsTmp = new JButton[5];
			
			for (int j = 0; j < buttonsTmp.length; j++) {
				
				if(manager.DoubleJeopardy){ //adjust # for DJ
					buttonsTmp[j] = new JButton(new Integer((j+1)*20).toString());
				}
				else{
					buttonsTmp[j] = new JButton(new Integer((j+1)*10).toString());
				}
				
				buttonsTmp[j].setFont(new Font("Impact", Font.PLAIN, 22));
				if(j<1){ //first button
					buttonsTmp[j].setBounds(10, ((categoryLabels[i].getY() + categoryLabels[i].getHeight()) + buttonBuffer), categoryWidth-20, 85);
				}
				else{    //the rest of them
					buttonsTmp[j].setBounds(10, ((buttonsTmp[j-1].getY() + buttonsTmp[j-1].getHeight()) + buttonBuffer),categoryWidth-20, 85);
				}
				
				//These final vars are basically work-arounds so I can reference a button from within it's own ActionListener
				//it's kinda janky...
				final JPanel tmpPanel = categoryPanels[i]; //category panel
				final int btnNum = j;
				final int catNum = i;
				
				ActionListener l = new ActionListener() {	
					@Override
					public void actionPerformed(ActionEvent arg0) {
						tmpPanel.getComponent(btnNum+1).setVisible(false);
						manager.openQ(catNum, btnNum);
					}
				};
				buttonsTmp[j].addActionListener(l);
				
				categoryPanels[i].add(buttonsTmp[j]);
			}
			
			
		}
	}
}
